package com.netistate.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.netistate.base.BaseAction;
import com.netistate.pojo.Project;
import com.netistate.pojo.User;
import com.netistate.service.ProjectService;
import com.netistate.service.UserService;
import com.netistate.util.GetMd5;

public class UserAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	private ProjectService projectService;
	
	private User user;
	private String err;
	
	private String username;
	private String password;
	private String realName;
	private String email;
	private String tel;
	private String id;
	
	private String newpassword;
	private String oldpassword;
	
	private List<User> userList = new ArrayList<User>();
	private List<Project> projectList = new ArrayList<Project>();

	/**
	 * 加载即跳转index-success
	 */
	@Override
	@Action("index")
	public String execute() throws Exception {
		return SUCCESS;
	}
	/**
	 * 登录验证
	 * @return
	 * @throws Exception
	 */
	@Action(value="login",results={@Result(name="success",type="redirectAction",
											params={"actionName","main"}),
									@Result(name="error",type="redirectAction",
											params={"actionName","index","err","${err}"}),
									@Result(name="input",type="dispatcher",location="index-success.jsp")})
	public String login() throws Exception{
		
		User u = userService.findUserByUsernameAndPwd(user);
		if(u != null){
			//用户存在
			getSession().put("session_user", u);
			
			//登录即查找项目，并按项目名称首字母排序
			projectList = projectService.findAll();
			getSession().put("projectList", projectList);
			//登录后默认查找一个月内的项目信息
			List<Project> projectListInOneMonth = projectService.findProjectListInOneMonth();
			getSession().put("projectListInOneMonth", projectListInOneMonth);
			return SUCCESS;
		} else {
			err = "-1";
			return ERROR;
		}
	}
	
	/**
	 * 非空验证
	 */
	public void validateLogin(){
		if(user == null || user.getUsername() == null || "".equals(user.getUsername())){
			addActionError("账号不能为空！");
		}
		if(user == null || user.getPassword() == null || "".equals(user.getPassword())){
			addActionError("密码不能为空!");
		}
	}

	/**
	 * 登录后跳转这里
	 * @return
	 * @throws Exception
	 */
	@Action("main")
	public String main() throws Exception{
		
		//获取全部的用户
		userList = userService.findAll();
		getSession().put("userList", userList);
		return SUCCESS;
	}
	
	/**
	 * 安全退出
	 * @return
	 * @throws Exception
	 */
	@Action(value="exit")
	public String exit() throws Exception{
		//销毁session，最后用流把javaScript代码刷过去，直接跳转至登录页
		HttpSession session = getRequest().getSession();
		session.invalidate();
		getResponse().getWriter().print("<script type='text/javascript'>");
		getResponse().getWriter().print("window.parent.location.href='/netistate-pms/index.action'");
		getResponse().getWriter().print("</script>");
		return null;
	}
	
	/**
	 * 添加用户
	 */
	@Override
	@Action("add_user")
	public String save() throws Exception {
		
		String mdPassword = GetMd5.getMd5(password);
		
		User u = new User();
		u.setUsername(username);
		u.setPassword(mdPassword);
		u.setEmail(email);
		u.setTel(tel);
		u.setRealName(realName);
		
		userService.save(u);
		
		return null;
	}
	
	/**
	 * 删除用户操作
	 */
	@Override
	@Action("delete_user")
	public String del() throws Exception {
		int uid = Integer.parseInt(id);
		System.out.println("uid:"+uid);
		userService.del(uid);
		return null;
	}
	
	/**
	 * 点击取得用户信息
	 */
	@Override
	@Action("edit_user")
	public String edit() throws Exception {
		
		int uid = Integer.parseInt(id);
		User u = userService.findUserById(uid);
		getSession().put("editUser", u);
		return null;
	}
	
	/**
	 * 用户修改密码
	 * @return
	 * @throws Exception
	 */
	@Action("user_editPassword")
	public String user_editPassword() throws Exception{
		
		User u = userService.findUserById(Integer.parseInt(id));
		
		String mdPassword = GetMd5.getMd5(newpassword);
		
		u.setPassword(mdPassword);
		
		userService.save(u);
		
		return null;
	}
	
	
	/**
	 * 保存修改的用户信息
	 * @return
	 * @throws Exception
	 */
	@Action("editsave_user")
	public String editsave() throws Exception {
		int uid = Integer.parseInt(id);
		User u = userService.findUserById(uid);
		u.setUsername(username);
		u.setRealName(realName);
		u.setTel(tel);
		u.setEmail(email);		
		userService.editsave(u);
		
		return null;
	}

	/**
	 * 查询所有用户
	 */
	@Override
	@Action(value="findAllUser",results={@Result(name="success",type="dispatcher",location="/jsp/user/userManager.jsp")})
	public String findAll() throws Exception {
		List<User> userList = userService.findAll();
		getRequest().setAttribute("uList", userList);
		getSession().put("allUser", userList);
		return SUCCESS;
	}

	@Override
	public String findById() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	//get set
	
	public User getUser() {
		return user;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getErr() {
		return err;
	}
	public void setErr(String err) {
		this.err = err;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	public List<Project> getProjectList() {
		return projectList;
	}
	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getNewpassword() {
		return newpassword;
	}
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
	public String getOldpassword() {
		return oldpassword;
	}
	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Autowired
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
}
